<?php


namespace app\api\controller\v1;


use app\api\model\BikeModel;
use app\common\controller\UserBaseController;
use app\common\library\TBitEcuModel;
use app\common\library\XiaoAnEcuModel;

class EcutestController extends UserBaseController
{
    function infoAction()
    {
        $this->paramlist = [
            'imei' => 'optstring',
            'sn' => 'optstring',
        ];
        $this->check_params();
        $this->check_token();
        $imei = $this->argv['imei'];
        $ecu_sn = $this->argv['sn'];

        $info = null;
        if (!empty($ecu_sn)) {
            $info = (new BikeModel())->get_ecu_info_by_sn($ecu_sn);
        } else {
            $info = (new BikeModel())->get_ecu_info_by_imei($imei);
        }

        if (empty($info)) {
            $this->throw_myexception(ERROR_QUERY_INFO, "查询信息失败！");
        }

        $ecuinfo = [
            'ecu_sn' => $info['sn'],
            'ecu_model' => $info['model'],
            'bluetooth_token' => $info['bluetooth_token'],
        ];

        $this->resp['info'] = $ecuinfo;

        return $this->ret_resp();
    }

    function lockAction()
    {
        $this->paramlist = [
            'imei' => 'string',
            'sn' => 'optstring',
        ];

        $this->check_params();
        $this->check_token();
        $imei = $this->argv['imei'];
        $ecu_sn = $this->argv['sn'];

        $bike_info = null;
        if (!empty($this->argv['imei'])) {
            $bike_info = BikeModel::instance()->get_info_by_imei($imei);
        } else {
            $bike_info = BikeModel::instance()->get_info_by_ecu_sn($ecu_sn);
        }

        if (!empty($bike_info) && $bike_info['inv_state'] == BIKE_INV_STATE_IN_MARKET) {
            //throw_myexception(X_ERROR, "已投放车辆不能执行测试操作！");
        }

        $tbit = false;
        if (!empty($bike_info)) {
            $imei = $bike_info['imei'];
            $ecu_sn = $bike_info['ecu_sn'];
            if ($bike_info['ecu_brand'] == 'TBIT') {
                $tbit = true;
            } else {
                $tbit = false;
            }
        } else {
            $ecu_info = null;
            if (!empty($ecu_sn)) {
                $ecu_info = (new BikeModel())->get_ecu_info_by_sn($ecu_sn);
            } else {
                $ecu_info = (new BikeModel())->get_ecu_info_by_imei($imei);
            }
            if (!empty($ecu_info)) {
                $tbit = $ecu_info['brand'] == 'TBIT' ? true : false;
            } else {
                $tbit = empty($imei);
            }
        }

        $ret = null;
        if ($tbit) {
            $ret = (new TBitEcuModel())->lock($ecu_sn);
        } else {
            $ret = (new XiaoAnEcuModel())->xiao_an_ecu_lock($imei);
        }

        if ($ret[0] != 0) {
            $this->throw_myexception(X_ERROR, $ret[1]);
        }

        return $this->ret_resp();
    }

    function unlockAction()
    {
        $this->paramlist = [
            'imei' => 'string',
            'sn' => 'optstring',
        ];

        $this->check_params();
        $this->check_token();
        $imei = $this->argv['imei'];
        $ecu_sn = $this->argv['sn'];

        $bike_info = null;
        if (!empty($this->argv['imei'])) {
            $bike_info = BikeModel::instance()->get_info_by_imei($imei);
        } else {
            $bike_info = BikeModel::instance()->get_info_by_ecu_sn($ecu_sn);
        }

        if (!empty($bike_info) && $bike_info['inv_state'] == BIKE_INV_STATE_IN_MARKET) {
            //throw_myexception(X_ERROR, "已投放车辆不能执行测试操作！");
        }


        $tbit = false;
        if (!empty($bike_info)) {
            $imei = $bike_info['imei'];
            $ecu_sn = $bike_info['ecu_sn'];
            if ($bike_info['ecu_brand'] == 'TBIT') {
                $tbit = true;
            } else {
                $tbit = false;
            }
        } else {
            $ecu_info = null;
            if (!empty($ecu_sn)) {
                $ecu_info = (new BikeModel())->get_ecu_info_by_sn($ecu_sn);
            } else {
                $ecu_info = (new BikeModel())->get_ecu_info_by_imei($imei);
            }
            if (!empty($ecu_info)) {
                $tbit = $ecu_info['brand'] == 'TBIT' ? true : false;
            } else {
                $tbit = empty($imei);
            }
        }

        $ret = null;

        $ret = null;
        if ($tbit) {
            $ret = (new TBitEcuModel())->unlock($ecu_sn);
        } else {
            $ret = (new XiaoAnEcuModel())->xiao_an_ecu_unlock($imei);
        }


        if ($ret[0] != 0) {
            $this->throw_myexception(X_ERROR, $ret[1]);
        }

        return $this->ret_resp();
    }

    function acc_onAction()
    {
        $this->paramlist = [
            'imei' => 'string',
            'sn' => 'optstring',
        ];

        $this->check_params();
        $this->check_token();
        $imei = $this->argv['imei'];
        $ecu_sn = $this->argv['sn'];

        $bike_info = null;
        if (!empty($this->argv['imei'])) {
            $bike_info = BikeModel::instance()->get_info_by_imei($imei);
        } else {
            $bike_info = BikeModel::instance()->get_info_by_ecu_sn($ecu_sn);
        }

        if (!empty($bike_info) && $bike_info['inv_state'] == BIKE_INV_STATE_IN_MARKET) {
            //throw_myexception(X_ERROR, "已投放车辆不能执行测试操作！");
        }


        $ret = null;

        $tbit = false;
        if (!empty($bike_info)) {
            $imei = $bike_info['imei'];
            $ecu_sn = $bike_info['ecu_sn'];
            if ($bike_info['ecu_brand'] == 'TBIT') {
                $tbit = true;
            } else {
                $tbit = false;
            }
        } else {
            $ecu_info = null;
            if (!empty($ecu_sn)) {
                $ecu_info = (new BikeModel())->get_ecu_info_by_sn($ecu_sn);
            } else {
                $ecu_info = (new BikeModel())->get_ecu_info_by_imei($imei);
            }
            if (!empty($ecu_info)) {
                $tbit = $ecu_info['brand'] == 'TBIT' ? true : false;
            } else {
                $tbit = empty($imei);
            }
        }

        if ($tbit) {
            $ret = (new TBitEcuModel())->acc_on($ecu_sn);
        } else {
            $ret = (new XiaoAnEcuModel())->xiao_an_ecu_acc_on($imei);
        }

        if ($ret[0] != 0) {
            $this->throw_myexception(X_ERROR, $ret[1]);
        }

        return $this->ret_resp();
    }

    function acc_offAction()
    {
        $this->paramlist = [
            'imei' => 'string',
            'sn' => 'optstring',
        ];

        $this->check_params();
        $this->check_token();
        $imei = $this->argv['imei'];
        $ecu_sn = $this->argv['sn'];

        $bike_info = null;
        if (!empty($this->argv['imei'])) {
            $bike_info = BikeModel::instance()->get_info_by_imei($imei);
        } else {
            $bike_info = BikeModel::instance()->get_info_by_ecu_sn($ecu_sn);
        }

        if (!empty($bike_info) && $bike_info['inv_state'] == BIKE_INV_STATE_IN_MARKET) {
            //throw_myexception(X_ERROR, "已投放车辆不能执行测试操作！");
        }

        $ret = null;
        $tbit = false;
        if (!empty($bike_info)) {
            $imei = $bike_info['imei'];
            $ecu_sn = $bike_info['ecu_sn'];
            if ($bike_info['ecu_brand'] == 'TBIT') {
                $tbit = true;
            } else {
                $tbit = false;
            }
        } else {
            $ecu_info = null;
            if (!empty($ecu_sn)) {
                $ecu_info = (new BikeModel())->get_ecu_info_by_sn($ecu_sn);
            } else {
                $ecu_info = (new BikeModel())->get_ecu_info_by_imei($imei);
            }
            if (!empty($ecu_info)) {
                $tbit = $ecu_info['brand'] == 'TBIT' ? true : false;
            } else {
                $tbit = empty($imei);
            }
        }

        if ($tbit) {
            $ret = (new TBitEcuModel())->acc_off($ecu_sn);
        } else {
            $ret = (new XiaoAnEcuModel())->xiao_an_ecu_acc_off($imei);
        }

        if ($ret[0] != 0) {
            $this->throw_myexception(X_ERROR, $ret[1]);
        }

        return $this->ret_resp();
    }

    function battery_unlockAction()
    {
        $this->paramlist = [
            'imei' => 'string',
            'sn' => 'optstring',
        ];

        $this->check_params();
        $this->check_token();
        $imei = $this->argv['imei'];
        $ecu_sn = $this->argv['sn'];

        $bike_info = null;
        if (!empty($this->argv['imei'])) {
            $bike_info = BikeModel::instance()->get_info_by_imei($imei);
        } else {
            $bike_info = BikeModel::instance()->get_info_by_ecu_sn($ecu_sn);
        }

        if (!empty($bike_info) && $bike_info['inv_state'] == BIKE_INV_STATE_IN_MARKET) {
            // throw_myexception(X_ERROR, "已投放车辆不能执行测试操作！");
        }

        $ret = null;
        $tbit = false;
        if (!empty($bike_info)) {
            $imei = $bike_info['imei'];
            $ecu_sn = $bike_info['ecu_sn'];
            if ($bike_info['ecu_brand'] == 'TBIT') {
                $tbit = true;
            } else {
                $tbit = false;
            }
        } else {
            $ecu_info = null;
            if (!empty($ecu_sn)) {
                $ecu_info = (new BikeModel())->get_ecu_info_by_sn($ecu_sn);
            } else {
                $ecu_info = (new BikeModel())->get_ecu_info_by_imei($imei);
            }
            if (!empty($ecu_info)) {
                $tbit = $ecu_info['brand'] == 'TBIT' ? true : false;
            } else {
                $tbit = empty($imei);
            }
        }

        if ($tbit) {
            $ret = (new TBitEcuModel())->unlock_battery($ecu_sn);
        } else {
            $ret = (new XiaoAnEcuModel())->unlock_battery($imei);
        }

        if ($ret[0] != 0) {
            $this->throw_myexception(X_ERROR, $ret[1]);
        }

        return $this->ret_resp();
    }

    function battery_lockAction()
    {
        $this->paramlist = [
            'imei' => 'string',
            'sn' => 'optstring',
        ];

        $this->check_params();
        $this->check_token();
        $imei = $this->argv['imei'];
        $ecu_sn = $this->argv['sn'];

        $bike_info = null;
        if (!empty($this->argv['imei'])) {
            $bike_info = BikeModel::instance()->get_info_by_imei($imei);
        } else {
            $bike_info = BikeModel::instance()->get_info_by_ecu_sn($ecu_sn);
        }

        if (!empty($bike_info) && $bike_info['inv_state'] == BIKE_INV_STATE_IN_MARKET) {
            // throw_myexception(X_ERROR, "已投放车辆不能执行测试操作！");
        }

        $ret = null;
        $tbit = false;
        if (!empty($bike_info)) {
            $imei = $bike_info['imei'];
            $ecu_sn = $bike_info['ecu_sn'];
            if ($bike_info['ecu_brand'] == 'TBIT') {
                $tbit = true;
            } else {
                $tbit = false;
            }
        } else {
            $ecu_info = null;
            if (!empty($ecu_sn)) {
                $ecu_info = (new BikeModel())->get_ecu_info_by_sn($ecu_sn);
            } else {
                $ecu_info = (new BikeModel())->get_ecu_info_by_imei($imei);
            }
            if (!empty($ecu_info)) {
                $tbit = $ecu_info['brand'] == 'TBIT' ? true : false;
            } else {
                $tbit = empty($imei);
            }
        }

        if ($tbit) {
            $ret = (new TBitEcuModel())->lock_battery($ecu_sn);
        } else {
            $ret = (new XiaoAnEcuModel())->unlock_battery($imei);
        }

        if ($ret[0] != 0) {
            $this->throw_myexception(X_ERROR, $ret[1]);
        }

        return $this->ret_resp();
    }

    function voiceAction()
    {
        $this->paramlist = [
            'imei' => 'string',
            'sn' => 'optstring',
            'index' => 'int',
        ];

        $this->check_params();
        $this->check_token();
        $imei = $this->argv['imei'];
        $ecu_sn = $this->argv['sn'];

        $bike_info = null;
        if (!empty($this->argv['imei'])) {
            $bike_info = BikeModel::instance()->get_info_by_imei($imei);
        } else {
            $bike_info = BikeModel::instance()->get_info_by_ecu_sn($ecu_sn);
        }

        if (!empty($bike_info) && $bike_info['inv_state'] == BIKE_INV_STATE_IN_MARKET) {
            //throw_myexception(X_ERROR, "已投放车辆不能执行测试操作！");
        }


        $ret = null;
        $index = $this->argv['index'];
        $tbit = false;
        if (!empty($bike_info)) {
            $imei = $bike_info['imei'];
            $ecu_sn = $bike_info['ecu_sn'];
            if ($bike_info['ecu_brand'] == 'TBIT') {
                $tbit = true;
            } else {
                $tbit = false;
            }
        } else {
            $ecu_info = null;
            if (!empty($ecu_sn)) {
                $ecu_info = (new BikeModel())->get_ecu_info_by_sn($ecu_sn);
            } else {
                $ecu_info = (new BikeModel())->get_ecu_info_by_imei($imei);
            }
            if (!empty($ecu_info)) {
                $tbit = $ecu_info['brand'] == 'TBIT' ? true : false;
            } else {
                $tbit = empty($imei);
            }
        }

        if ($tbit) {
            $map = [
                ECU_VOICE_TYPE_RING => TBitEcuModel::TBIT_VOICE_TYPE_RING,
                ECU_VOICE_TYPE_ABOUT_TO_OSVC => TBitEcuModel::TBIT_VOICE_TYPE_OSVC,
                ECU_VOICE_TYPE_OSVC => TBitEcuModel::TBIT_VOICE_TYPE_OSVC_ALEADY,
                ECU_VOICE_TYPE_PARKWARN =>TBitEcuModel::TBIT_VOICE_TYPE_PARKWARN,
                ECU_VOICE_TYPE_OVERSPEED => TBitEcuModel::TBIT_VOICE_TYPE_OVERSPEED,
            ];
            $t = 0;
            if (!isset($map[$index])) {
                $t = 0;
            } else {
                $t = $map[$index];
            }
            $ret = (new TBitEcuModel())->device_voice($ecu_sn, $t);
        } else {
            $map = [
                ECU_VOICE_TYPE_RING => XiaoAnEcuModel::XIAO_AN_VOICE_TYPE_RING,
                ECU_VOICE_TYPE_ABOUT_TO_OSVC => XiaoAnEcuModel::XIAO_AN_VOICE_TYPE_OSVC,
                ECU_VOICE_TYPE_OSVC => XiaoAnEcuModel::XIAO_AN_VOICE_TYPE_OSVC_ALEADY,
                ECU_VOICE_TYPE_PARKWARN =>XiaoAnEcuModel::XIAO_AN_VOICE_TYPE_PARKWARN,
                ECU_VOICE_TYPE_OVERSPEED => XiaoAnEcuModel::XIAO_AN_VOICE_TYPE_OVERSPEED,
            ];
            $t = 0;
            if (!isset($map[$index])) {
                $t = 0;
            } else {
                $t = $map[$index];
            }
            $ret = (new XiaoAnEcuModel())->device_voice($imei, $index);
        }

        if ($ret[0] != 0) {
            $this->throw_myexception(X_ERROR, $ret[1]);
        }

        return $this->ret_resp();
    }

    function batAction()
    {
        $this->paramlist = [
            'imei' => 'string',
            'sn' => 'optstring',
        ];


        $this->check_params();
        $this->check_token();

        $imei = $this->argv['imei'];
        $ecu_sn = $this->argv['sn'];

        $bike_info = null;
        if (!empty($this->argv['imei'])) {
            $bike_info = BikeModel::instance()->get_info_by_imei($imei);
        } else {
            $bike_info = BikeModel::instance()->get_info_by_ecu_sn($ecu_sn);
        }

        if (!empty($bike_info) && $bike_info['inv_state'] == BIKE_INV_STATE_IN_MARKET) {
            //throw_myexception(X_ERROR, "已投放车辆不能执行测试操作！");
        }

        $tbit = false;
        if (!empty($bike_info)) {
            $imei = $bike_info['imei'];
            $ecu_sn = $bike_info['ecu_sn'];
            if ($bike_info['ecu_brand'] == 'TBIT') {
                $tbit = true;
            } else {
                $tbit = false;
            }
        } else {
            if (empty($imei)) {
                $tbit = true;
            } else {
                $tbit = false;
            }
        }

        if (!$tbit) {
            $ret = (new XiaoAnEcuModel())->xiao_an_ecu_unlock($imei);
            if ($ret[0] != 0) {
                $this->throw_myexception(X_ERROR, $ret[1]);
            }
            sleep(3);
            $ret = (new XiaoAnEcuModel())->xiao_an_ecu_lock($imei);
            if ($ret[0] != 0) {
                $this->throw_myexception(X_ERROR, $ret[1]);
            }
            sleep(3);
            $ret = (new XiaoAnEcuModel())->xiao_an_ecu_acc_on($imei);
            if ($ret[0] != 0) {
                $this->throw_myexception(X_ERROR, $ret[1]);
            }
            sleep(3);
            $ret = (new XiaoAnEcuModel())->xiao_an_ecu_acc_off($imei);
            if ($ret[0] != 0) {
                $this->throw_myexception(X_ERROR, $ret[1]);
            }

            sleep(3);
            $ret = (new XiaoAnEcuModel())->unlock_battery($imei);
            if ($ret[0] != 0) {
                $this->throw_myexception(X_ERROR, $ret[1]);
            }
        } else {
            $ret = (new TBitEcuModel())->unlock($ecu_sn);
            if ($ret[0] != 0) {
                $this->throw_myexception(X_ERROR, $ret[1]);
            }
            sleep(3);


            $ret = (new TBitEcuModel())->lock($ecu_sn);
            if ($ret[0] != 0) {
                $this->throw_myexception(X_ERROR, $ret[1]);
            }
            sleep(3);
            $ret = (new TBitEcuModel())->acc_off($ecu_sn);
            if ($ret[0] != 0) {
                $this->throw_myexception(X_ERROR, $ret[1]);
            }
            sleep(3);
            $ret = (new TBitEcuModel())->acc_on($ecu_sn);
            if ($ret[0] != 0) {
                $this->throw_myexception(X_ERROR, $ret[1]);
            }
            sleep(3);
            $ret = (new TBitEcuModel())->unlock_battery($ecu_sn);
        }

        return $this->ret_resp();
    }

}
